﻿
using System.Linq;
using System.Collections.Generic;

namespace Tools.com.Pager
{
    public static class PageLinqExtensions
    {
        /// <summary>
        /// 泛型 IQueryable 内部分页
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="allItems">集合</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">每页总数</param>
        /// <returns></returns>
        public static PagedList<T> ToPagedList<T>
            (
                this IQueryable<T> allItems,
                int pageIndex,
                int pageSize
            )
        {
            if (pageIndex < 1)
                pageIndex = 1;
            var itemIndex = (pageIndex-1) * pageSize;
            var pageOfItems = allItems.Skip(itemIndex).Take(pageSize);
            var totalItemCount = allItems.Count();
            return new PagedList<T>(pageOfItems, pageIndex, pageSize, totalItemCount);
        }

        /// <summary>
        /// 集合 List 内部分页
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="allItems">集合</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">每页总数</param>
        /// <returns></returns>
        public static PagedList<T> ToPagedList<T>
            (
                this IList<T> allItems,
                int pageIndex,
                int pageSize
            )
        {
            if (pageIndex < 1)
                pageIndex = 1;
            var itemIndex = (pageIndex - 1) * pageSize;
            var pageOfItems = allItems.Skip(itemIndex).Take(pageSize);
            var totalItemCount = allItems.Count();
            return new PagedList<T>(pageOfItems, pageIndex, pageSize, totalItemCount);
        }

        /// <summary>
        /// 泛型 IQueryable 外部分页
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="allItems">集合</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">每页总数</param>
        /// <param name="totalCount">总条数</param>
        /// <returns></returns>
        public static PagedList<T> ToPagedList<T>
           (
               this IQueryable<T> allItems,
               int pageIndex,
               int pageSize,
               int totalCount
           )
        {
            if (pageIndex < 1)
                pageIndex = 1;
            var pageOfItems = allItems;
            var totalItemCount = totalCount;
            return new PagedList<T>(pageOfItems, pageIndex, pageSize, totalItemCount);
        }

        /// <summary>
        /// 集合 List 外部分页
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="allItems">集合</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">每页总数</param>
        /// <param name="totalCount">总条数</param>
        /// <returns></returns>
        public static PagedList<T> ToPagedList<T>
            (
                this IList<T> allItems,
                int pageIndex,
                int pageSize,
                int totalCount
            )
        {
            if (pageIndex < 1)
                pageIndex = 1;
            var pageOfItems = allItems;
            var totalItemCount = totalCount;
            return new PagedList<T>(pageOfItems, pageIndex, pageSize, totalItemCount);
        }


    }
}
